package com.hxzy.util;

import com.alibaba.fastjson.JSONObject;
import com.hxzy.vo.JsonResult;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

/**
 * @author nick
 * @description 通用工具类
 * @date create in 2020/9/11
 */
public class CommonUtil {
    public static final String PATTERN1 = "yyyy-MM-dd HH:mm:ss";
    public static final String PATTERN2 = "yyyy-MM-dd";
    public static final String PATTERN3 = "HH:mm:ss";
    public static final String PATTERN4 = "yyyy/MM/dd";
    private static final long SECOND=1000;
    private static final long MINUTE=SECOND*60;
    private static final long HOUR=MINUTE*60;
    private static final long DAY=HOUR*24;


    //将LocalDateTime转换为毫秒
    public static long localDateTime2mills(LocalDateTime localDateTime) {
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }
    //将LocalDateTime转换为Timestamp
    public static Timestamp localDateTime2TimeStamp(LocalDateTime localDateTime){
        long time = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return new Timestamp(time);
    }

    //将LocalDateTime转换为指定格式的字符串
    public static String localDateTime2String(LocalDateTime localDateTime, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return localDateTime.format(formatter);
    }

    //将字符串转换为LocalDateTime
    public static LocalDateTime str2LocalDateTime(String str,String pattern){
        DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern);
        return LocalDateTime.parse(str, df);
    }

    public static LocalDateTime timeStamp2LocalDateTime(Timestamp timestamp) {
        // 时间戳转LocalDateTime         LocalDateTime.ofInstant(表示瞬间[毫秒],时区[中国时区编号为Asia/Shanghai])
        return timestamp.toLocalDateTime();
    }

    public static String timeToString(LocalDateTime localDateTime) {
        long l = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        long now = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        if (l-now>0){
            throw new RuntimeException("发布的时间不能比当前时间大");
        }
        long time= now-l;
        String str=null;
        if(time < MINUTE){
            str="刚刚";
        }else if (time < HOUR){
            int minute= (int) (time/MINUTE);
            str=minute+"分钟前";
        }else if (time < DAY){
            int hour= (int) (time / HOUR);
            str=hour+"小时前";
        }else {
            int day= (int) (time / DAY);
            str=day+"天前";
        }
        return str;
    }

    /**
     * 将object对象转化json,并且返回给客户端
     * @param resp
     * @param result
     * @throws IOException
     */
    public static void back(HttpServletResponse resp, Object result) throws IOException {
        String string = JSONObject.toJSONString(result);
        PrintWriter writer = resp.getWriter();
        writer.write(string);
        writer.flush();
        writer.close();
    }
}
